package edu.bit.water.schedule;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Response;
import edu.bit.water.controller.MetaEvaWuseInfoController;
import edu.bit.water.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;;
import java.util.Objects;

@Component
@EnableScheduling
public class DataFetch {

    protected static final Logger logger = LoggerFactory.getLogger(MetaEvaWuseInfoController.class);

    String token = "";

//    @Scheduled(cron = "0 0 0/1 * * ?")
//    @Scheduled(cron = "0/3 * * * * ?")
    public void getToken() throws IOException {
        String url = "https://s1.anylink.io:8443/user/getToken";
        JSONObject param = new JSONObject();
        param.put("tenantEname", "longge");
        param.put("name", "longge");
        param.put("password", "longge01");
        param.put("hash", "1234");
        logger.info("调用getToken接口的参数："+param.toJSONString());
        Response response = HttpUtil.formJsonPost(url, param.toString());
        if (response !=null) {
            JSONObject resultObj = JSONObject.parseObject(Objects.requireNonNull(response).body().string());
            logger.info("调用getToken接口的返回值："+resultObj);
            token = resultObj.getString("data");
            logger.info("返回的token："+token);
        } else {
            logger.info("调用getToken接口的异常");
        }
    }

//    @Scheduled(cron = "0/4 * * * * ?")
    public void getAgentList() {
        String url= "https://s1.anylink.io:8443/agentList/pagination?agentId=1405825&condition=1&page=1&perPage=1000000&token="+token;
        Response response = HttpUtil.formJsonGet(url);
//        System.out.println("test中的token是：" + response); // 可以获取到
    }
}
